/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CompraScanner.java
 *
 * Created on 21/05/2011, 11:19:50 AM
 */
package GUI;

import Interfaz.InterfazControl;
import Motor.Cliente;
import Motor.Producto;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Vane
 */
public class CompraScanner extends javax.swing.JFrame {

	private InterfazControl laInterfaz;
	/** Creates new form CompraScanner */
	private List<Producto> productosSelecionados = new ArrayList<Producto>();
	private int total = 0;
	private String laSucursal="";

	public CompraScanner(InterfazControl laInterfaz,String sucursal) {
		this.laInterfaz = laInterfaz;
		initComponents();
		laSucursal=sucursal;
		this.setLocationRelativeTo(null); // Cmabiar icono de java en ventana
		this.setIconImage(new ImageIcon("src/imagenes/marketplus2.png").getImage());
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	private void llenarTabla(Producto producto) {
		String[] columna = {"Código", "Descripcion", "Precio"};
		DefaultTableModel dtm = new DefaultTableModel(null, columna);
	}

	//ArrayList<Producto> Lista= this.laInterfaz.todosLosProductos();
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jTCodigoProducto = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTCodigoScanner = new javax.swing.JTextField();
        jTCodigoProductoEliminar = new javax.swing.JTextField();
        jbEliminar = new javax.swing.JToggleButton();
        jTTotal = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTabla = new javax.swing.JTable();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jTGenerarCodigo = new javax.swing.JToggleButton();
        jProcesar = new javax.swing.JToggleButton();
        jLabel7 = new javax.swing.JLabel();
        jBGenerarFactura = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jTCedula = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable2);

        jScrollPane4.setViewportView(jTree1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(680, 400));
        setResizable(false);
        getContentPane().setLayout(null);

        jTCodigoProducto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTCodigoProductoActionPerformed(evt);
            }
        });
        jTCodigoProducto.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTCodigoProductoKeyTyped(evt);
            }
        });
        getContentPane().add(jTCodigoProducto);
        jTCodigoProducto.setBounds(190, 170, 80, 20);

        jLabel1.setText("Código Producto:");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(30, 170, 140, 20);

        jLabel2.setText("Código Producto a Eliminar:");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(30, 200, 200, 14);

        jLabel3.setText("Total BsF + Iva:");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(30, 230, 120, 14);

        jTCodigoScanner.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTCodigoScannerActionPerformed(evt);
            }
        });
        getContentPane().add(jTCodigoScanner);
        jTCodigoScanner.setBounds(190, 260, 80, 20);

        jTCodigoProductoEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTCodigoProductoEliminarActionPerformed(evt);
            }
        });
        jTCodigoProductoEliminar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTCodigoProductoEliminarKeyTyped(evt);
            }
        });
        getContentPane().add(jTCodigoProductoEliminar);
        jTCodigoProductoEliminar.setBounds(190, 200, 80, 20);

        jbEliminar.setText("Eliminar");
        jbEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbEliminarActionPerformed(evt);
            }
        });
        getContentPane().add(jbEliminar);
        jbEliminar.setBounds(300, 200, 130, 23);

        jTTotal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTTotalActionPerformed(evt);
            }
        });
        jTTotal.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTTotalKeyTyped(evt);
            }
        });
        getContentPane().add(jTTotal);
        jTTotal.setBounds(190, 230, 80, 20);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel4.setText("SCANNER");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(280, 50, 100, 20);

        jTabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Código", "Producto", "Precio"
            }
        ));
        jScrollPane3.setViewportView(jTabla);

        getContentPane().add(jScrollPane3);
        jScrollPane3.setBounds(440, 110, 220, 220);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 10));
        jLabel5.setText("Lista de Productos");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(490, 70, 110, 15);

        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/pantallatactil.jpg"))); // NOI18N
        jLabel6.setText("jLabel6");
        getContentPane().add(jLabel6);
        jLabel6.setBounds(30, 10, 130, 110);

        jTGenerarCodigo.setText("Generar Código");
        jTGenerarCodigo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTGenerarCodigoActionPerformed(evt);
            }
        });
        getContentPane().add(jTGenerarCodigo);
        jTGenerarCodigo.setBounds(300, 260, 130, 23);

        jProcesar.setText("Procesar");
        jProcesar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jProcesarActionPerformed(evt);
            }
        });
        getContentPane().add(jProcesar);
        jProcesar.setBounds(300, 170, 130, 23);

        jLabel7.setText("Generar Código:");
        getContentPane().add(jLabel7);
        jLabel7.setBounds(30, 260, 100, 20);

        jBGenerarFactura.setText("Generar  Factura");
        jBGenerarFactura.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBGenerarFacturaActionPerformed(evt);
            }
        });
        getContentPane().add(jBGenerarFactura);
        jBGenerarFactura.setBounds(130, 300, 170, 40);

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel8.setText("RIF: J-122231245          Desarrollado por: Group Delivery");
        getContentPane().add(jLabel8);
        jLabel8.setBounds(190, 350, 320, 14);

        jTCedula.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTCedulaActionPerformed(evt);
            }
        });
        jTCedula.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTCedulaKeyTyped(evt);
            }
        });
        getContentPane().add(jTCedula);
        jTCedula.setBounds(190, 140, 80, 20);

        jLabel9.setText("Ingresar Cédula:");
        getContentPane().add(jLabel9);
        jLabel9.setBounds(30, 140, 140, 20);

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void jTCodigoProductoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTCodigoProductoActionPerformed
		//ODO add your handling code here:
	}//GEN-LAST:event_jTCodigoProductoActionPerformed

	private void jbEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbEliminarActionPerformed
		// TODO add your handling code here:
		if (jTCodigoProductoEliminar.getText().isEmpty()) {
			JOptionPane.showMessageDialog(null, "Introduzca Código Producto");
		} else {
			String idProd = this.jTCodigoProductoEliminar.getText();
			jTCodigoProductoEliminar.setText("");
			int nuevo;

			// VALIDAR  QUE SE META UN CODIGO DE PRODUCTO CORRECTO d lista
			if (productosSelecionados != null) {
				for (Producto producto : productosSelecionados) {
					if (producto.getIdProducto() == Integer.parseInt(idProd)) {
						productosSelecionados.remove(producto);
						nuevo = producto.getPrecioVenta();
						total = total - nuevo;
						llenarTabla();
					}
				}
				jTTotal.setText(Integer.toString(total));
			}

		}
	}//GEN-LAST:event_jbEliminarActionPerformed

	private void jTCodigoScannerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTCodigoScannerActionPerformed
		// TODO add your handling code here:
	}//GEN-LAST:event_jTCodigoScannerActionPerformed

	private void jTCodigoProductoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTCodigoProductoKeyTyped
		// TODO add your handling code here:char c = evt.getKeyChar();
		char c = evt.getKeyChar();
		if (Character.isDigit(c) == false) {
			evt.consume();
		}



	}//GEN-LAST:event_jTCodigoProductoKeyTyped

	private void jTCodigoProductoEliminarKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTCodigoProductoEliminarKeyTyped
		// TODO add your handling code here:
		char c = evt.getKeyChar();
		if (Character.isDigit(c) == false) {
			evt.consume();
		}
	}//GEN-LAST:event_jTCodigoProductoEliminarKeyTyped
//
	private void jTTotalKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTTotalKeyTyped
		// TODO add your handling code here:
	}//GEN-LAST:event_jTTotalKeyTyped

	private void jTTotalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTTotalActionPerformed
		// TODO add your handling code here:
	}//GEN-LAST:event_jTTotalActionPerformed

	private void jTCodigoProductoEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTCodigoProductoEliminarActionPerformed
		// TODO add your handling code here:
	}//GEN-LAST:event_jTCodigoProductoEliminarActionPerformed

	private void jTGenerarCodigoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTGenerarCodigoActionPerformed
		// TODO add your handling code here:
	}//GEN-LAST:event_jTGenerarCodigoActionPerformed

	private void jProcesarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jProcesarActionPerformed

		if (jTCodigoProducto.getText().isEmpty()) {
			JOptionPane.showMessageDialog(null, "Introduzca Código Producto");
		} else {
			String id = this.jTCodigoProducto.getText();
			jTCodigoProducto.setText("");
			int productoInt = Integer.parseInt(id);
			int nuevo;
			Producto elProducto = laInterfaz.buscarProducto(productoInt);
			if (elProducto != null) {
				productosSelecionados.add(elProducto);
				llenarTabla();
				nuevo = elProducto.getPrecioVenta();
				total = total + nuevo;
			}
			if (elProducto == null) {
				JOptionPane.showMessageDialog(null, "Código de Producto Invalido");
			}
			// System.out.println("productoINY" + productosSelecionados.get(0));
			jTTotal.setText(Integer.toString(total));
		}
	}//GEN-LAST:event_jProcesarActionPerformed

	private void jBGenerarFacturaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBGenerarFacturaActionPerformed
		// TODO add your handling code here:
		// cambio de prueba
		if (!jTCedula.getText().isEmpty())
		{
			Cliente elCliente = laInterfaz.buscarCliente(Integer.parseInt(jTCedula.getText()));

			if (elCliente!=null)
			{
				Factura irFacturaScanner = new Factura(laInterfaz, elCliente, productosSelecionados,laSucursal,null);
				irFacturaScanner.setVisible(true);
				this.dispose();
			}
			if (elCliente==null)
				JOptionPane.showMessageDialog(null, "Cedula Invalida");
		}
		if (jTCedula.getText().isEmpty())
			JOptionPane.showMessageDialog(null, "Introduzca Cédula");
	}//GEN-LAST:event_jBGenerarFacturaActionPerformed

	private void jTCedulaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTCedulaActionPerformed
		// TODO add your handling code here:
	}//GEN-LAST:event_jTCedulaActionPerformed

	private void jTCedulaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTCedulaKeyTyped
		// TODO add your handling code here:
	}//GEN-LAST:event_jTCedulaKeyTyped

	private void llenarTabla() {
		String[] columna = {"Código", "Descripcion", "Precio"};
		DefaultTableModel dtm = new DefaultTableModel(null, columna);
		//ArrayList<Producto> Lista= this.laInterfaz.todosLosProductos();
		// ArrayList<Producto> Lista = this.laInterfaz.todosLosProductosSeleccionados();

		for (Producto pro : productosSelecionados) {
			String[] row = {Integer.toString(pro.getIdProducto()), pro.getNombreProducto(), Integer.toString(pro.getPrecioVenta())};
			dtm.addRow(row);
		}
		jTabla.setModel(dtm);
	}
	/**
	 * @param args the command line arguments
	 */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBGenerarFactura;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JToggleButton jProcesar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTextField jTCedula;
    private javax.swing.JTextField jTCodigoProducto;
    private javax.swing.JTextField jTCodigoProductoEliminar;
    private javax.swing.JTextField jTCodigoScanner;
    private javax.swing.JToggleButton jTGenerarCodigo;
    private javax.swing.JTextField jTTotal;
    private javax.swing.JTable jTabla;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTree jTree1;
    private javax.swing.JToggleButton jbEliminar;
    // End of variables declaration//GEN-END:variables
}
